﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace PlutoStudio.Web.Mvc.Html
{
    public static class NavigationExtensions
    {
        public static MvcHtmlString NavigationMenu<TModel>(this HtmlHelper<TModel> htmlHelper)
        {
            var sb = new StringBuilder();
            sb.AppendLine("<ul class=\"nav nav-list\">");
            var navigations = Navigations.NavigationDiscover.Navigations;
            foreach (var item in navigations)
            {
                htmlHelper.NavigationMenu(item, sb);
            }
            sb.AppendLine("</ul>");
            return MvcHtmlString.Create(sb.ToString());
        }

        private static void NavigationMenu<TModel>(this HtmlHelper<TModel> htmlHelper, Navigations.ControllerNavigation navigation, StringBuilder sb)
        {
            if (navigation.Actions.Count(action => action.Attribute.ShowInMenu) == 0)
            {
                return;
            }
            var controller = Navigations.NavigationDiscover.GetNavigation(htmlHelper.ViewContext.RouteData.Values["controller"].ToString());
            var isActive = controller != null && controller == navigation;
            var hasSubMenu = navigation.Actions.Count(action => action.Attribute.ShowInMenu) > 1;
            var li = new TagBuilder("li");
            if (isActive)
            {
                li.AddCssClass("active");
                if (hasSubMenu)
                {
                    li.AddCssClass("open");
                }
            }
            sb.AppendLine(li.ToString(TagRenderMode.StartTag));
            var a = new TagBuilder("a");
            if (hasSubMenu)
            {
                a.MergeAttribute("href", "#");
                a.AddCssClass("dropdown-toggle");
            }
            else
            {
                a.MergeAttribute("href", (htmlHelper.ViewContext.Controller as Controller).Url.Action(navigation.Actions.First().ActionName, navigation.ControllerName).ToString());
            }
            sb.AppendLine(a.ToString(TagRenderMode.StartTag));
            sb.AppendFormat("<i class=\"{0}\"></i>", navigation.Attribute.IconClass);
            sb.AppendFormat("<span class=\"menu-text\"> {0} </span>", navigation.Attribute.Title);
            if (hasSubMenu)
            {
                sb.AppendLine("<b class=\"arrow fa fa-angle-down\"></b>");
            }
            sb.AppendLine(a.ToString(TagRenderMode.EndTag));
            sb.AppendLine("<b class=\"arrow\"></b>");
            if (hasSubMenu)
            {
                sb.AppendLine("<ul class=\"submenu\">");
                foreach (var item in navigation.Actions.Where(action => action.Attribute.ShowInMenu))
                {
                    htmlHelper.NavigationMenu(item, sb);
                }
                sb.AppendLine("</ul>");
            }
            sb.AppendLine(li.ToString(TagRenderMode.EndTag));
        }


        private static void NavigationMenu<TModel>(this HtmlHelper<TModel> htmlHelper, Navigations.ActionNavigation navigation, StringBuilder sb)
        {
            var controller = Navigations.NavigationDiscover.GetNavigation(htmlHelper.ViewContext.RouteData.Values["controller"].ToString());
            var controllerAuth=navigation.Controller.Descriptor.GetFilterAttributes(true).FirstOrDefault(a => a is IAuthorizationFilter) as IAuthorizationFilter;
            var user = htmlHelper.ViewContext.HttpContext.User;
            var authorization = (navigation.Descriptor.GetFilterAttributes(true).FirstOrDefault(a => a is IAuthorizationFilter) as IAuthorizationFilter)??controllerAuth;
            if (authorization!=null && !authorization.HasPermission(user))
            {
                return;
            }
            var li = new TagBuilder("li");
            if (controller != null && controller == navigation.Controller)
            {
                var action = controller.Actions.SingleOrDefault(act => act.ActionName.ToLower() == htmlHelper.ViewContext.RouteData.Values["action"].ToString().ToLower());
                if (action != null && (action == navigation || action.Attribute.Title.Equals(navigation.Attribute.Title)))
                {
                    li.AddCssClass("active");
                }
            }
            sb.AppendLine(li.ToString(TagRenderMode.StartTag));
            sb.AppendLine(htmlHelper.ActionLink(navigation.Attribute.Title, navigation.ActionName, navigation.Controller.ControllerName).ToString());
            sb.AppendLine("<b class=\"arrow\"></b>");
            sb.AppendLine(li.ToString(TagRenderMode.EndTag));
        }


        public static MvcHtmlString Breadcrumb<TModel>(this HtmlHelper<TModel> htmlHelper)
        {
            var sb = new StringBuilder();
            var cd = new ReflectedControllerDescriptor(htmlHelper.ViewContext.Controller.GetType());
            var actionName = htmlHelper.ViewContext.RouteData.Values["action"].ToString();
            var action = cd.FindAction(htmlHelper.ViewContext.Controller.ControllerContext, actionName);
            sb.AppendLine("<ul class=\"breadcrumb\">");
            sb.AppendLine("<li>");
            sb.AppendLine("<i class=\"ace-icon fa fa-home home-icon\"></i>");
            if (cd.ControllerName.ToLower().Equals("home") && action.ActionName.ToLower().Equals("index"))
            {
                sb.AppendLine("首页");

            }
            else
            {
                sb.AppendLine("<a href=\"/\">首页</a>");

            }
            sb.AppendLine("</li>");
            var controllerBreadcrumb = cd.GetFilterAttributes(true).SingleOrDefault(a => a is Navigations.BreadcrumbAttribute) as Navigations.BreadcrumbAttribute;
            if (controllerBreadcrumb != null && !cd.ControllerName.ToLower().Equals("home"))
            {
                sb.AppendLine("<li>");
                if (!string.IsNullOrWhiteSpace(controllerBreadcrumb.IconClass))
                {
                    sb.AppendFormat("<i class=\"{0}\"></i>", controllerBreadcrumb.IconClass);
                    sb.AppendLine();
                }
                if (action.ActionName.ToLower().Equals("index"))
                {
                    sb.AppendLine(controllerBreadcrumb.Title);
                }
                else
                {
                    sb.AppendLine(htmlHelper.ActionLink(controllerBreadcrumb.Title, "Index").ToString());

                } sb.AppendLine("</li>");
            }
            var actionBreadcrumb = action.GetFilterAttributes(true).SingleOrDefault(a => a is Navigations.BreadcrumbAttribute) as Navigations.BreadcrumbAttribute;
            if (actionBreadcrumb != null && !action.ActionName.ToLower().Equals("index"))
            {
                sb.AppendLine("<li>");
                if (!string.IsNullOrWhiteSpace(controllerBreadcrumb.IconClass))
                {
                    sb.AppendFormat("<i class=\"{0}\"></i>", controllerBreadcrumb.IconClass);
                    sb.AppendLine();
                }
                sb.AppendLine(actionBreadcrumb.Title);
                sb.AppendLine("</li>");
            }
            sb.AppendLine("</ul>");
            return MvcHtmlString.Create(sb.ToString());
        }

        //<ul class="breadcrumb">
        //                <li>
        //                    <i class="ace-icon fa fa-home home-icon"></i>
        //                    <a href="#">Home</a>
        //                </li>

        //                <li>
        //                    <a href="#">Other Pages</a>
        //                </li>
        //                <li class="active">Blank Page</li>
        //            </ul>
    }
}
